package com.tjhzzx.hnceCore2024.util;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.logging.Logger;

/**
 * @author Luc
 * @version 1.0
 * @date 2020-06-12 15:52
 */
public class ExcelUtil {

    private static final Logger LOGGER = Logger.getLogger(ExcelUtil.class.getName());

    public static void exportExcel(Collection<?> dataSet, Class<?> pojoClass, String path, String fileName, HttpServletResponse response) throws IOException {
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(null, "Sheet1"), pojoClass, dataSet);
        downLoadExcel(path, fileName, workbook, response);
    }

    public static void exportExcel(List<Map<String, Object>> list, List<ExcelExportEntity> headers, String path, String fileName, HttpServletResponse response) throws IOException {
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(null, "Sheet1"), headers, list);
        downLoadExcel(path, fileName, workbook, response);
    }

    public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException {
        if (StringUtils.isBlank(filePath)) {
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        params.setNeedSave(false);
//        params.setNeedSave(true);
//        params.setSaveUrl("/excel/");
        try {
            return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
        } catch (NoSuchElementException e) {
            throw new IOException("模板不能为空");
        } catch (Exception e) {
            throw new IOException(e.getMessage());
        }
    }

    private static void downLoadExcel(String path, String fileName, Workbook workbook, HttpServletResponse response) throws IOException {
        FileOutputStream fos = new FileOutputStream(path + "/" + fileName);
        workbook.write(fos);
        fos.close();
        response.setContentType("application/pdf;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName).getBytes(), "iso-8859-1"));
        OutputStream os = null;
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path + "/" + fileName));
            byte[] b = new byte[bis.available() + 1000];
            int i = 0;
            os = response.getOutputStream();
            while ((i = bis.read(b)) != -1) {
                os.write(b, 0, i);
            }
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    enum ExcelTypeEnum {
        XLS("xls"), XLSX("xlsx");
        private String value;

        ExcelTypeEnum(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }
}
